﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DragonBones;

public class PlayerMovement : MonoBehaviour {

	public float speed = 2f;

	private AnimationControl anim;
	private string currentState = "idle";

	void Start () {
		anim = GetComponent<AnimationControl> ();
	}

	void Update () {

		int h = (int)Input.GetAxisRaw ("moveHorizontal");
		int v = (int)Input.GetAxisRaw ("moveVertical");
		Move (h,v);

		bool left = Input.GetKey (KeyCode.LeftArrow);
		bool right = Input.GetKey (KeyCode.RightArrow);
		bool up = Input.GetKey (KeyCode.UpArrow);
		bool down = Input.GetKey (KeyCode.DownArrow);

		bool _left = Input.GetKey(KeyCode.A);
		bool _right = Input.GetKey(KeyCode.D);
		bool _up = Input.GetKey(KeyCode.W);
		bool _down = Input.GetKey(KeyCode.S);

		if (Input.GetKeyDown (KeyCode.LeftArrow)) {
			currentState = "left";
		}

		if (Input.GetKeyDown (KeyCode.RightArrow)) {
			currentState = "right";
		}

		if (Input.GetKeyDown (KeyCode.UpArrow)) {
			currentState = "back";
		}

		if (Input.GetKeyDown (KeyCode.DownArrow)) {
			currentState = "front";
		}

		if (Input.GetKeyUp (KeyCode.LeftArrow) || Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.UpArrow) || Input.GetKeyUp (KeyCode.DownArrow)) {
			if (left || right) {
				if (up || down) {
					if (up) {
						currentState = "back";
					} else {
						currentState = "front";
					}
				} else {
					if (right) {
						currentState = "right";
					} else {
						currentState = "left";
					}
				}
			}else{
				if (up) {
					currentState = "back";
				} else {
					currentState = "front";
				}
			}
		}

		UpdateAnim (left, right, up, down, _left, _right, _up, _down);

	}

	void Move(int h,int v) {

		Vector3 move = new Vector3 (h, v, 0f);
		this.transform.position = Vector3.Lerp (this.transform.position, this.transform.position + move, speed * Time.deltaTime);
	}

	void UpdateAnim (bool left,bool right,bool up,bool down ,bool _left,bool _right,bool _up,bool _down){
		bool headbool = (left == false && right == false && up == false && down == false);
		bool bodybool = (_left == false && _right == false && _up == false && _down == false);
		if (headbool && !bodybool) {
			if (_left || _right) {
				if (_up || _down) {
					if (_up == _down) {
						anim.UpdateAnimation ("idle", "idle");
					} else if (_up) {
						anim.UpdateAnimation ("back", "back");
					} else {
						anim.UpdateAnimation ("front", "front");
					}
				} else {
					if (_right == _left) {
						anim.UpdateAnimation ("idle", "idle");
					} else if (_left) {
						anim.UpdateAnimation ("left", "left");
					} else {
						anim.UpdateAnimation ("right", "right");
					}
				}
			} else {
				if (_up || _down) {
					if (_up == _down) {
						anim.UpdateAnimation ("idle", "idle");
					} else if (_up) {
						anim.UpdateAnimation ("back", "back");
					} else {
						anim.UpdateAnimation ("front", "front");
					}
				}
			}
		} else if (!headbool && bodybool) {
			if (left || right || up || down) {
				anim.UpdateAnimation (currentState, "idle");
//				if (up || down) {
//					if (up == false && down == false) {
//						anim.UpdateAnimation ("idle", "idle");
//					} else {
//						anim.UpdateAnimation (currentState, "idle");
//					}
//				} else {
//					if (right == false && left == false) {
//						anim.UpdateAnimation ("idle", "idle");
//					} else {
//						anim.UpdateAnimation (currentState, "idle");
//					}
//				}
//			}else{
//				if (up || down) {
//					if (up == false && down == false) {
//						anim.UpdateAnimation ("idle", "idle");
//					} else {
//						anim.UpdateAnimation (currentState, "idle");
//					}
//				}
			}
		} else if (!headbool && !bodybool) {
//			int headint = -1;//up 0, down 1,right 3, left 2
			int bodyint = -1;
			if (_left || _right) {
				if (_up || _down) {
					if (_up == _down) {
						bodyint = -1;
					}else if (_up) {
						bodyint = 0;
					} else {
						bodyint = 1;
					}
				} else {
					if (_left == _right) {
						bodyint = -1;
					}else if (_left) {
						bodyint = 2;
					} else {
						bodyint = 3;
					}
				}
			}else{
				if (_up == _down) {
					bodyint = -1;
				} else if (_up) {
					bodyint = 0;
				} else {
					bodyint = 1;
				}
			}
//
//			if (left || right) {
//				if (up || down) {
//					if (up) {
//						headint = 0;
//					} else {
//						headint = 1;
//					}
//				} else {
//					if (left) {
//						headint = 2;
//					} else {
//						headint = 3;
//					}
//				}
//			}else{
//					if (up || down) {
//						if (up) {
//							headint = 0;
//						} else {
//							headint = 1;
//						}
//					}
//			}

			switch (bodyint) {
			case -1:
				anim.UpdateAnimation (currentState, "idle");
				break;
			case 0:
				anim.UpdateAnimation (currentState, "back");
				break;
			case 1:
				anim.UpdateAnimation (currentState, "front");
				break;
			case 2:
				anim.UpdateAnimation (currentState, "left");
				break;
			case 3:
				anim.UpdateAnimation (currentState, "right");
				break;
			}
		} else {
			anim.UpdateAnimation ("idle", "idle");
		}
	}
}
